package junitTests;

import static org.junit.Assert.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;

import upload.UploadAudio;
import delete.DeleteMediaServlet;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class DeleteMediaTest {

	private final LocalServiceTestHelper helper = 
			new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
	
	@Before
	public void prepare() {
		helper.setUp();
	}
	/*
	 * 
	 */
	@Test
	public void DatastoreReducedByOne() {
		// initialise the datastore and the keys
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		BlobKey blobKey = new BlobKey("ABC123");
		HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "testfile", "audio", blobKey);
		// test that there is one media file in the datastore
		Query query = new Query("Audio", audioFileKey);
		assertEquals(1, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
		Entity mediaFile;
		try {
			mediaFile = ds.get(audioFileKey);
			DeleteMediaServlet.deleteMediaFile(mediaFile, blobKey);
		} catch (EntityNotFoundException e) {
			fail("EntityNotFoundException!");
		}
		assertEquals(0, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
	}
	/*
	 * 
	 */
	@Test //(expected = EntityNotFoundException.class)
	public void BlobDeleted() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		BlobKey blobKey = new BlobKey("ABC123");
		HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "testfile", "audio", blobKey);
        Query query = new Query("Audio", audioFileKey);
		assertEquals(1, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
		Entity mediaFile = null;
		try {
			mediaFile = ds.get(audioFileKey);
			DeleteMediaServlet.deleteMediaFile(mediaFile, blobKey);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals(null, mediaFile);
	}
	/*
	 * 
	 */
	@Test (expected = EntityNotFoundException.class)
	public void DatastoreEntityDeleted() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		BlobKey blobKey = new BlobKey("ABC123");
		HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "testfile", "audio", blobKey);
	}
	/*
	 * 
	 */
	@Test
	public void DeleteConfirmation() {
		fail();
	}
}
